<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE xsl:stylesheet SYSTEM "entities.dtd">
<xsl:stylesheet version="1.0" exclude-result-prefixes="xsl" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:rdfs="http://www.w3.org/2000/01/rdf-schema#" xmlns:owl="http://www.w3.org/2002/07/owl#" xmlns:xsd="http://www.w3.org/2001/XMLSchema#" xmlns:dm="http://dm.rdlfacade.org/data#" xmlns:rdl="http://rdl.rdlfacade.org/data#" xmlns:tpl="http://tpl.rdlfacade.org/data#" xmlns:oim="http://oim.rdlfacade.org/data#" xmlns:qmxf="http://ns.ids-adi.org/qxf/model#" xmlns:qxl="http://ns.ids-adi.org/qxf/literal#" xmlns:qxf="http://ns.ids-adi.org/qxf/schema#">
	<xsl:output method="xml" indent="yes" encoding="UTF-8" doctype-system="schema.dtd" omit-xml-declaration="no"/>
	<xsl:strip-space elements="*"/>
	<xsl:template name="identification">
		<xsl:param name="lang"/>
		<xsl:param name="identifier"/>
		<xsl:param name="identified"/>
		<xsl:param name="context"/>
		<xsl:message terminate="yes">IDENTIFICATION CANNOT BE USED UNTIL WE HAVE PROPER LINKAGE TO IDC PLUS CLASSES ETC.</xsl:message>
	</xsl:template>
	<xsl:template name="definition">
		<xsl:param name="lang"/>
		<xsl:param name="represented"/>
		<xsl:param name="pattern"/>
		<xsl:param name="base"/>
		<xsl:comment>
		    	Add a definition (@note form is liable to change).</xsl:comment>
		<!--
			@todo this really needs to be altered into a
			dm: template, once we get that going - this
			should be OK for now in Camelot project.
		-->
		<qxf:relationship instance-of="{$base}">
			<qxf:property instance-of="http://dm.rdlfacade.org/data#hasRepresented" reference="{$represented}"/>
			<qxf:property instance-of="http://dm.rdlfacade.org/data#hasPattern" as="http://www.w3.org/2001/XMLSchema#string" lang="{$lang}">
				<xsl:value-of select="$pattern"/>
			</qxf:property>
		</qxf:relationship>
	</xsl:template>
	<xsl:template name="annotation">
		<xsl:param name="lang"/>
		<xsl:param name="value"/>
		<xsl:param name="type">&xsd;#string</xsl:param>
		<xsl:param name="annotated"/>
		<xsl:param name="property"/>
		<qxf:relationship instance-of="http://tpl.rdlfacade.org/data#R99514676016">
			<qxf:property instance-of="http://tpl.rdlfacade.org/data#R49608530151" as="{$type}">
				<xsl:if test="string-length(string($lang)) &gt; 0">
					<xsl:attribute namespace="http://www.w3.org/XML/1998/namespace" name="xml:lang">
						<xsl:value-of select="$lang"/>
					</xsl:attribute>
				</xsl:if>
				<xsl:value-of select="$value"/>
			</qxf:property>
			<qxf:property instance-of="http://tpl.rdlfacade.org/data#R71961093547" reference="{$annotated}"/>
			<qxf:property instance-of="http://tpl.rdlfacade.org/data#R25980496885" reference="{$property}"/>
		</qxf:relationship>
	</xsl:template>
	<xsl:template match="qmxf:designation">
		<xsl:message terminate="yes">DESIGNATIONS CANNOT BE USED UNTIL WE HAVE A PROPER SCHEME AND SCOPE</xsl:message>
		<xsl:call-template name="identification">
			<xsl:with-param name="identifier" select="string(.)"/>
			<xsl:with-param name="identified" select="string(../@id)"/>
			<xsl:with-param name="scheme">&rdl;#ISO15926Designation</xsl:with-param>
			<xsl:with-param name="scope">&iso;</xsl:with-param>
		</xsl:call-template>
	</xsl:template>
	<xsl:template match="qmxf:suggested-designation">
		<xsl:message terminate="yes">SUGGESTED DESIGNATIONS CANNOT BE USED UNTIL WE HAVE A PROPER SCHEME AND SCOPE</xsl:message>
		<xsl:call-template name="identification">
			<xsl:with-param name="identifier" select="string(.)"/>
			<xsl:with-param name="identified" select="string(../@id)"/>
			<xsl:with-param name="scheme">&rdl;#ISO15926Designation</xsl:with-param>
			<xsl:with-param name="scope" select="string(../@id)"/>
		</xsl:call-template>
	</xsl:template>
	<xsl:template match="qmxf:name">
		<xsl:if test="string(.) != ''">
			<xsl:comment>
				Add an rdfs:label (@note these might end getting
				elided once we put the data in proper template
				instances - then they will get optionally
				regenerated by the QXF -> OWL/RDF).</xsl:comment>
			<xsl:call-template name="annotation">
				<xsl:with-param name="lang" select="string(@xml:lang)"/>
				<xsl:with-param name="value" select="string(.)"/>
				<xsl:with-param name="annotated" select="string(../@id)"/>
				<xsl:with-param name="property">&rdfs;#label</xsl:with-param>
			</xsl:call-template>
		</xsl:if>
	</xsl:template>
	<xsl:template match="qmxf:description">
		<xsl:if test="string(.) != ''">
			<xsl:comment>
				Add an rdfs:comment (@note these might end getting
				elided once we put the data in proper template
				instances - then they will get optionally
				regenerated by the QXF -> OWL/RDF).</xsl:comment>
			<xsl:call-template name="annotation">
				<xsl:with-param name="lang" select="string(@xml:lang)"/>
				<xsl:with-param name="value" select="string(.)"/>
				<xsl:with-param name="annotated" select="string(../@id)"/>
				<xsl:with-param name="property">&rdfs;#comment</xsl:with-param>
			</xsl:call-template>
		</xsl:if>
	</xsl:template>
	<xsl:template match="qmxf:status">
		<xsl:choose>
			<xsl:when test="(.. = /qmxf:qxmf)">
				<xsl:comment>_____ Status _____</xsl:comment>
			</xsl:when>
			<xsl:otherwise>
				<xsl:comment>..... Status .....</xsl:comment>
			</xsl:otherwise>
		</xsl:choose>
		<qxf:relationship instance-of="http://tpl.rdlfacade.org/data#R20247551573">
			<qxf:property instance-of="http://tpl.rdlfacade.org/data#R64574858717" reference="{../@id}"/>
			<qxf:property instance-of="http://tpl.rdlfacade.org/data#R56599656536" reference="{@authority}"/>
			<qxf:property instance-of="http://tpl.rdlfacade.org/data#R61794465713" reference="{@class}"/>
			<xsl:choose>
				<xsl:when test="@from">
					<qxf:property instance-of="http://tpl.rdlfacade.org/data#R42656850346" as="http://www.w3.org/2001/XMLSchema#dateTime">
						<xsl:value-of select="@from"/>
					</qxf:property>
				</xsl:when>
				<xsl:when test="qmxf:qmxf/@timestamp">
					<qxf:property instance-of="http://tpl.rdlfacade.org/data#R42656850346" as="http://www.w3.org/2001/XMLSchema#dateTime">
						<xsl:value-of select="@from"/>
					</qxf:property>
				</xsl:when>
				<xsl:otherwise>
					<!-- @todo do nothing? -->
				</xsl:otherwise>
			</xsl:choose>
			<xsl:choose>
				<xsl:when test="@to">
					<qxf:property instance-of="http://tpl.rdlfacade.org/data#R28026830404" as="http://www.w3.org/2001/XMLSchema#dateTime">
						<xsl:value-of select="@to"/>
					</qxf:property>
				</xsl:when>
				<xsl:otherwise>
					<!-- @todo do nothing? -->
				</xsl:otherwise>
			</xsl:choose>
		</qxf:relationship>
	</xsl:template>
	<xsl:template match="qmxf:textual-definition">
		<xsl:if test="string(.) != ''">
			<xsl:call-template name="definition">
				<xsl:with-param name="lang" select="string(@xml:lang)"/>
				<xsl:with-param name="pattern" select="string(.)"/>
				<xsl:with-param name="represented" select="string(../@id)"/>
				<xsl:with-param name="base">&dm;#ClassOfDefinition</xsl:with-param>
			</xsl:call-template>
		</xsl:if>
	</xsl:template>
	<xsl:template name="common">
		<xsl:apply-templates select="qmxf:designation|
				qmxf:suggested-designation|
				qmxf:name|
				qmxf:description|
				qmxf:textual-definition
				"/>
	</xsl:template>
	<xsl:template match="qmxf:template-definition">
		<xsl:comment>_____ Template definition _____</xsl:comment>
		<xsl:comment>
			<xsl:value-of select="qmxf:name[1]"/>
		</xsl:comment>
		<!-- @todo reconsider this? -->
		<qxf:relationship instance-of="http://tpl.rdlfacade.org/data#R16376066707" id="{@id}">
			<qxf:property instance-of="http://tpl.rdlfacade.org/data#R16988346495" as="http://ns.ids-adi.org/qxf/literal#literal">
				<qxl:list>
					<xsl:for-each select="qmxf:role-definition">
						<qxl:item reference="{@id}"/>
					</xsl:for-each>
				</qxl:list>
			</qxf:property>
		</qxf:relationship>
		<xsl:call-template name="common"/>
		<xsl:variable name="count" select="count(qmxf:role-definition)"/>
		<xsl:for-each select="qmxf:role-definition">
			<xsl:variable name="index" select="position()"/>
			<xsl:variable name="next" select="../qmxf:role-definition[$index + 1]"/>
			<xsl:if test="not(@id)">
				<xsl:message terminate="yes">ERROR: no id on role definition</xsl:message>
			</xsl:if>
			<xsl:if test="not(@range)">
				<xsl:message terminate="yes">ERROR: no range on role definition</xsl:message>
			</xsl:if>
			<xsl:comment>..... Role definition .....</xsl:comment>
			<xsl:comment>
				<xsl:value-of select="qmxf:name[1]"/>
			</xsl:comment>
			<qxf:relationship id="{@id}" instance-of="http://tpl.rdlfacade.org/data#R74478971040">
				<xsl:choose>
					<!-- @todo lame check for data type property ... need to improve (make declarative?) -->
					<xsl:when test="starts-with(string(@range), 'http://www.w3.org/2001/XMLSchema') or
							(string(@range) = 'http://www.w3.org/2000/01/rdf-schema#Literal')">
						<!--
				    Declare it as a datatype property.
				-->
						<qxf:property instance-of="http://www.w3.org/1999/02/22-rdf-syntax-ns#type" reference="http://www.w3.org/2002/07/owl#DatatypeProperty"/>
					</xsl:when>
					<xsl:otherwise>
						<!--
				    Declare it as an object property.
				-->
						<qxf:property instance-of="http://www.w3.org/1999/02/22-rdf-syntax-ns#type" reference="http://www.w3.org/2002/07/owl#ObjectProperty"/>
						<!--
				    Declare it as an object property.
				    @note should check cardinality.
				-->
						<qxf:property instance-of="http://www.w3.org/1999/02/22-rdf-syntax-ns#type" reference="http://www.w3.org/2002/07/owl#FunctionalProperty"/>
					</xsl:otherwise>
				</xsl:choose>
				<qxf:property instance-of="http://www.w3.org/2000/01/rdf-schema#domain" reference="{../@id}"/>
				<qxf:property instance-of="http://www.w3.org/2000/01/rdf-schema#range" reference="{@range}"/>
				<qxf:property instance-of="http://tpl.rdlfacade.org/data#R97483568938" as="http://www.w3.org/2001/XMLSchema#int">
					<xsl:value-of select="$index"/>
				</qxf:property>
			</qxf:relationship>
			<xsl:call-template name="common"/>
			<xsl:if test="false">
				<xsl:variable name="node_id" select="concat(../@id, '_', $index)"/>
				<xsl:variable name="next_id">
					<xsl:choose>
						<xsl:when test="$next">
							<xsl:value-of select="concat(../@id, '_', $index + 1)"/>
						</xsl:when>
						<xsl:otherwise>&rdf;#nil</xsl:otherwise>
					</xsl:choose>
				</xsl:variable>
				<qxf:relationship id="{$node_id}" instance-of="http://www.w3.org/1999/02/22-rdf-syntax-ns#List">
					<qxf:property instance-of="http://www.w3.org/1999/02/22-rdf-syntax-ns#first" reference="{@id}"/>
					<qxf:property instance-of="http://www.w3.org/1999/02/22-rdf-syntax-ns#rest" reference="{$next_id}"/>
				</qxf:relationship>
			</xsl:if>
		</xsl:for-each>
		<xsl:apply-templates select="qmxf:status"/>
	</xsl:template>
	<xsl:template match="qmxf:template-qualification">
		<xsl:comment>_____ Template qualification _____</xsl:comment>
		<xsl:comment>
			<xsl:value-of select="qmxf:name[1]"/>
		</xsl:comment>
		<xsl:comment>
			Specialization.</xsl:comment>
		<qxf:relationship instance-of="http://dm.rdlfacade.org/data#Specialization">
			<qxf:property instance-of="http://dm.rdlfacade.org/data#hasSuperclass" reference="{@qualifies}"/>
			<qxf:property instance-of="http://dm.rdlfacade.org/data#hasSubclass" reference="{@id}"/>
		</qxf:relationship>
		<xsl:call-template name="common"/>
		<xsl:for-each select="qmxf:role-qualification">
			<xsl:comment>..... Role qualification .....</xsl:comment>
			<xsl:if test="qmxf:value/@reference">
				<xsl:comment>Reference restriction</xsl:comment>
				<qxf:relationship instance-of="http://tpl.rdlfacade.org/data#R40103148466">
					<qxf:property instance-of="http://tpl.rdlfacade.org/data#R49267603385" reference="{../@id}"/>
					<qxf:property instance-of="http://tpl.rdlfacade.org/data#R30741601855" reference="{@qualifies}"/>
					<qxf:property instance-of="http://tpl.rdlfacade.org/data#R21129944603" reference="{qmxf:value/@reference}"/>
				</qxf:relationship>
			</xsl:if>
			<xsl:if test="qmxf:value/@as">
				<xsl:comment>Value restriction</xsl:comment>
				<qxf:relationship instance-of="http://tpl.rdlfacade.org/data#R67036823327">
					<qxf:property instance-of="http://tpl.rdlfacade.org/data#R56456315674" reference="{../@id}"/>
					<qxf:property instance-of="http://tpl.rdlfacade.org/data#R89867215482" reference="{@qualifies}"/>
					<qxf:property instance-of="http://tpl.rdlfacade.org/data#R29577887690" as="{qmxf:value/@as}">
						<xsl:value-of select="string(qmxf:value)"/>
					</qxf:property>
				</qxf:relationship>
			</xsl:if>
			<xsl:if test="@range">
				<xsl:comment>Range restriction</xsl:comment>
				<qxf:relationship instance-of="http://tpl.rdlfacade.org/data#R76288246068">
					<qxf:property instance-of="http://tpl.rdlfacade.org/data#R99672026745" reference="{../@id}"/>
					<qxf:property instance-of="http://tpl.rdlfacade.org/data#R91125890543" reference="{@qualifies}"/>
					<qxf:property instance-of="http://tpl.rdlfacade.org/data#R98983340497" reference="{@range}"/>
				</qxf:relationship>
			</xsl:if>
		</xsl:for-each>
		<xsl:apply-templates select="qmxf:status"/>
	</xsl:template>
	<xsl:template name="has-entity-type">
		<!-- @todo need to support better-->
		<xsl:comment>
			Entity type should probably expressed in
			a more explicit way, but we will just do
			this for now.</xsl:comment>
		<qxf:relationship instance-of="http://www.w3.org/1999/02/22-rdf-syntax-ns#Statement">
			<qxf:property instance-of="http://www.w3.org/1999/02/22-rdf-syntax-ns#subject" reference="{../@id}"/>
			<qxf:property instance-of="http://www.w3.org/1999/02/22-rdf-syntax-ns#predicate" reference="http://www.w3.org/1999/02/22-rdf-syntax-ns#type"/>
			<qxf:property instance-of="http://www.w3.org/1999/02/22-rdf-syntax-ns#object" reference="{@reference}"/>
		</qxf:relationship>
	</xsl:template>
	<xsl:template name="is-instance-of">
		<!-- @todo consider making tpl-specific entries? -->
		<xsl:comment>
			Classification.</xsl:comment>
		<qxf:relationship instance-of="http://dm.rdlfacade.org/data#Classification">
			<qxf:property instance-of="http://dm.rdlfacade.org/data#hasClassifier" reference="{@reference}"/>
			<qxf:property instance-of="http://dm.rdlfacade.org/data#hasClassified" reference="{../@id}"/>
		</qxf:relationship>
	</xsl:template>
	<xsl:template name="is-subclass-of">
		<!-- @todo consider making tpl-specific entries? -->
		<xsl:comment>
			Specialization.</xsl:comment>
		<qxf:relationship instance-of="http://dm.rdlfacade.org/data#Specialization">
			<qxf:property instance-of="http://dm.rdlfacade.org/data#hasSuperclass" reference="{@reference}"/>
			<qxf:property instance-of="http://dm.rdlfacade.org/data#hasSubclass" reference="{../@id}"/>
		</qxf:relationship>
	</xsl:template>
	<xsl:template match="qmxf:class-definition">
		<xsl:comment>_____ Class definition _____</xsl:comment>
		<xsl:comment>
			<xsl:value-of select="qmxf:name[1]"/>
		</xsl:comment>
		<xsl:call-template name="common"/>
		<xsl:for-each select="qmxf:entity-type">
			<xsl:call-template name="has-entity-type"/>
		</xsl:for-each>
		<xsl:for-each select="qmxf:classification">
			<xsl:call-template name="is-instance-of"/>
		</xsl:for-each>
		<xsl:for-each select="qmxf:specialization">
			<xsl:call-template name="is-subclass-of"/>
		</xsl:for-each>
		<xsl:apply-templates select="qmxf:status"/>
	</xsl:template>
	<xsl:template match="/qmxf:qmxf">
		<qxf:qxf>
			<xsl:if test="count(@xml:base) &gt; 0">
				<xsl:attribute namespace="http://www.w3.org/XML/1998/namespace#" name="xml:base">
					<xsl:value-of select="@xml:base"/>
				</xsl:attribute>
			</xsl:if>
			<xsl:apply-templates/>
			<xsl:variable name="role-definitions" select="*/qmxf:role-definition[@id]"/>
			<xsl:for-each select="$role-definitions">
				<xsl:variable name="role-id" select="@id"/>
				<xsl:variable name="role-set" select="$role-definitions[@id = $role-id]"/>
				<xsl:if test="($role-set[position() = 1] = .) and (count($role-set) &gt; 1)">
					<xsl:message terminate="yes">
						Illegal use of role <xsl:value-of select="$role-id"/> in multiple definitions.  A role id can appear only in one template-definition.</xsl:message>
				</xsl:if>
			</xsl:for-each>
		</qxf:qxf>
	</xsl:template>
	<xsl:template match="/">
		<xsl:apply-templates/>
	</xsl:template>
	<xsl:template match="text()">
		<xsl:message terminate="yes">unhandled input text at <xsl:value-of select="name(..)"/>.</xsl:message>
	</xsl:template>
	<xsl:template match="@*">
		<xsl:message terminate="yes">unhandled input attribute <xsl:value-of select="name(.)"/>.</xsl:message>
	</xsl:template>
	<xsl:template match="*">
		<xsl:message terminate="yes">unhandled input element <xsl:value-of select="name(.)"/>.</xsl:message>
	</xsl:template>
</xsl:stylesheet>
